package org.jground.rx.ui.zk.viewControl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jground.rx.IRXObject;
import org.jground.rx.RX;
import org.jground.rx.RXObjectService;
import org.jground.rx.RXTypeForeach;
import org.jground.rx.RXView;
import org.jground.rx.ui.zk.UIFrame;
import org.jground.rx.ui.zk.UIViewPanel;
import org.jground.util.ArrayUtils;
import org.jground.zk.zulX.HboxX;
import org.jground.zk.zulX.VboxX;
import org.zkoss.zul.Label;

public class UIAttributeViewForeach extends UIAttributeViewBase {

    private static Log logger = LogFactory.getLog(UIAttributeViewForeach.class);

    public UIAttributeViewForeach createNew(UIFrame uiFrame, RXView rxControl) {
        return new UIAttributeViewForeach(uiFrame, rxControl);
    }

    //
    public UIAttributeViewForeach() {}

    private String[] compOids;
    private String compMoid;
    // private String attrefBack;

    private List<UIViewPanel> compViewPanels = new ArrayList<UIViewPanel>();
    private boolean numbering;
    private int currentNumberOfComponents;
    private VboxX vbox;

    private UIAttributeViewForeach(UIFrame uiFrame, RXView rxControl) {
        super(uiFrame, rxControl);
        logger.debug("init UIAttributeViewForeach");
        //
        RXTypeForeach rxTypeForeach = (RXTypeForeach) rxControl.getRXAttribute().getRXType().getBaseType();
        compMoid = rxTypeForeach.getCompType().getLookupMoid();
        // attrefBack = rxTypeForeach.getCompType().getAttrefBack();
        numbering = rxControl.hasProperty(RX.AttValue.numbering);
        vbox = new VboxX();
        add(vbox);
    }

    public void set(String... values) {
        clear();

        if (ArrayUtils.isNotEmpty(values)) {
            compOids = values;
            updateUI();
        }
    }

    private void updateUI() {

        List<IRXObject> comps = RXObjectService.getInstance().getByIds(compOids);
        currentNumberOfComponents = comps.size();
        int index = 0;
        vbox.removeAll();
        for (IRXObject mod : comps) {
            // String targetOid = mod.getCurrentValue(attrefBack);
            // IRXObject targetMod = IRXObject.getById(targetOid);
            if (compViewPanels.size() < currentNumberOfComponents) {
                compViewPanels.add(new UIViewPanel(uiFrame, compMoid));
            }
            UIViewPanel panel = compViewPanels.get(index);
            panel.setData(mod);
            if (numbering) {
                vbox.add(new HboxX(new Label(String.valueOf(index + 1)), panel.getView()));
            } else {
                // vbox.add(targetPanel);
                vbox.add(panel.getView());
            }
            index++;
        }
    }

    @Override
    public Collection<String> renderToString() {
        if (ArrayUtils.isNotEmpty(compOids)) {
            List<IRXObject> comps = RXObjectService.getInstance().getByIds(compOids);
            for (int index = 0; index < comps.size(); index++) {
                renderToStringS.add(uiFrame.getUISynopsisRendererFactory().toString(comps.get(index)));
            }
        }
        return renderToStringS;
    }

    @Override
    public void clear() {
        renderToStringS.clear();
        compOids = null;
        updateUI();
    }

}
